//
//  MYZonePickerView.m
//  HZTC_YaDeArt
//
//  Created by MayerF on 17/3/30.
//  Copyright © 2017年 HZTC. All rights reserved.
//

#import "MYZonePickerView.h"

#define kSeperatorViewH 30

typedef void(^DoneBack)(id result);

@interface MYZonePickerView()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, copy)DoneBack doneBack;
@property (nonatomic, strong)UIView *pSeperatorView;
@property (nonatomic, strong)UIView *pBgView;
@property (nonatomic, strong)UIButton *pDone;
@property (nonatomic, strong)UIPickerView *pPickerView;
@property (nonatomic, strong)NSArray *pProvinceArray;
@property (nonatomic, strong)NSArray *pCityArray;
@property (nonatomic, strong)NSArray *pDetailArray;
@property (nonatomic, assign)NSInteger nProvinceRow;
@property (nonatomic, assign)NSInteger nCityRow;
@property (nonatomic, assign)NSInteger nDetailRow;
@property (nonatomic, assign)NSInteger nComponent;
@end

@implementation MYZonePickerView
#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pSeperatorView = [UIView new];
    _pBgView = [UIView new];
    _pDone = [UIButton new];
    _pPickerView = [UIPickerView new];
    _pPickerView.dataSource = self;
    _pPickerView.delegate = self;
    
    _pSeperatorView.backgroundColor = kMYHexToRGB(0xe4e4e4, 1);
    _pBgView.backgroundColor = [UIColor whiteColor];
    [_pDone setTitle:@"完成" forState:UIControlStateNormal];
    [_pDone setTitleColor:kMYHexToRGB(0xf15059, 1) forState:UIControlStateNormal];
    [_pDone addTarget:self action:@selector(doneOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_pSeperatorView];
    [self addSubview:_pPickerView];
    [_pSeperatorView addSubview:_pBgView];
    [_pBgView addSubview:_pDone];
    [self p_initConstraints];
    
    _nZonePickerType = kZonePicker_City_Type;
    _nComponent = 2;
    _nProvinceRow = 0;
    _nCityRow = 0;
    _nDetailRow = 0;
    [self p_loadZoneFile];
    [self p_getTitles];
}

- (void)p_initConstraints
{
    [_pSeperatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(kSeperatorViewH);
    }];
    [_pBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pSeperatorView);
        make.left.mas_equalTo(_pSeperatorView);
        make.right.mas_equalTo(_pSeperatorView);
        make.bottom.mas_equalTo(_pSeperatorView).offset(-0.5);
    }];
    [_pDone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pBgView);
        make.right.mas_equalTo(_pBgView).offset(-10);
        make.bottom.mas_equalTo(_pBgView);
        make.width.mas_equalTo(50);
    }];
    [_pPickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pSeperatorView.mas_bottom);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

- (void)p_loadZoneFile
{
    NSString *pFilePath = [[NSBundle mainBundle] pathForResource:@"city.json" ofType:nil];
    NSArray *zoneArray = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:pFilePath] options:NSJSONReadingMutableContainers error:nil];
    
    NSMutableArray *pTempPovince = [NSMutableArray array];
    NSMutableArray *pTempCity = [NSMutableArray array];
    NSMutableArray *pTempDetail = [NSMutableArray array];

    //解析简单的 city.json结构
    for (NSDictionary *dict in zoneArray) {
        NSString *province = [dict valueForKey:@"name"];
        NSArray *citys = [dict valueForKey:@"city"];
        [pTempPovince addObject:province];
        [pTempCity addObject:citys];
    }
    
    
    /* //用于解析复杂的 zone.json结构
    for (NSDictionary *dict in zoneArray) {
        NSString *proviceKey = [dict.allKeys firstObject];
        [pTempPovince addObject:proviceKey];
        NSArray *cityArray = [dict valueForKey:proviceKey];
        NSMutableArray *pTempCityArray = [NSMutableArray array];
        NSMutableArray *pDetailArray = [NSMutableArray array];
        
        for (NSDictionary *city in cityArray) {
            NSString *cityKey = [city.allKeys firstObject];
            [pTempCityArray addObject:cityKey];
            NSArray *detailArray = [city valueForKey:cityKey];
            [pDetailArray addObject:detailArray];
        }
        [pTempCity addObject:pTempCityArray];
        [pTempDetail addObject:pDetailArray];
    }
     */
    
    _pProvinceArray = pTempPovince;
    _pCityArray = pTempCity;
    _pDetailArray = pTempDetail;
}

- (void)p_getTitles
{
    switch (_nZonePickerType) {
        case kZonePicker_City_Type:
        {
            NSMutableArray *pTemp = [_pCityArray mutableCopy];
            for (int i = 0; i < _pCityArray.count; i++) {
                NSArray *citys = _pCityArray[i];
                if (citys.count == 1) {
                    NSArray *details = [_pDetailArray[i] firstObject];
                    [pTemp replaceObjectAtIndex:i withObject:details];
                }
            }
            _pCityArray = pTemp;
        }
            break;
        case kZonePicker_DetailAddress_Type:
        {
            _nComponent = 3;
        }
            break;
            
        default:
            break;
    }
}

- (void)doneClickCompleted:(void(^)(id result))completed
{
    _doneBack = completed;
}

#pragma mark -
#pragma mark action
- (void)doneOnClick
{
    NSMutableArray *pTempArray = [NSMutableArray arrayWithCapacity:3];
    NSString *strPro = _pProvinceArray[_nProvinceRow];
    NSString *strCity = _pCityArray[_nProvinceRow][_nCityRow];
    [pTempArray addObject:strPro];
    [pTempArray addObject:strCity];
    if (_nZonePickerType == kZonePicker_DetailAddress_Type) {
        NSString *strDetail = _pDetailArray[_nProvinceRow][_nCityRow][_nDetailRow];
        [pTempArray addObject:strDetail];
    }
    _doneBack(pTempArray);
}
#pragma mark - 
#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return _nComponent;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component > _nComponent) { return 0;}
    if (component == 0) {
        return _pProvinceArray.count;
    }else if (component == 1) {
        NSArray *cityArray = _pCityArray[_nProvinceRow];
        return cityArray.count;
    }else {
        NSArray *detailArray = _pDetailArray[_nProvinceRow][_nCityRow];
        return detailArray.count;
    }
}

- (NSString *)p_getTitle:(NSInteger)row component:(NSInteger)component
{
    if (component > _nComponent) { return nil;}
    if (component == 0) {
        return _pProvinceArray[row];
    }else if (component == 1) {
        NSArray *cityArray = _pCityArray[_nProvinceRow];
        return cityArray[row];
    }else {
        NSArray *detailArray = _pDetailArray[_nProvinceRow][_nCityRow];
        return detailArray[row];
    }
}

- (void)p_resetOriginRowWithPickerView:(UIPickerView *)pickerView Component:(NSInteger)component
{
    component++;
    while (component < _nComponent) {
        [pickerView selectRow:0 inComponent:component animated:YES];
        component++;
    }
}
#pragma mark -
#pragma mark UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [self p_getTitle:row component:component];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *pickerLabel = (UILabel *)view;
    if (!pickerLabel) {
        pickerLabel = [UILabel new];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        pickerLabel.font = [UIFont boldSystemFontOfSize:17];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component > _nComponent) { return;}
    if (component == 0) {
        _nProvinceRow = row;
        _nCityRow = _nDetailRow = 0;
    }else if (component == 1) {
        _nCityRow = row;
        _nDetailRow = 0;
    }else if (component == 2){
        _nDetailRow = row;
    }
    [pickerView reloadAllComponents];
    [self p_resetOriginRowWithPickerView:pickerView Component:component];
}

@end
